<?php

class fop_IndexController extends Zend_Controller_Action
{
  function init() {
	$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/ressources/fop/style.css');
  }

  function indexAction() {

    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','table','list');
//print_r($data);
//exit;
	$this->view->listTables = unserialize($data);
	$this->saveBreadcrumbs('','','');
    
  } //end IndexAction

  function createnewtableAction() {
  	$name = $this->_getParam('tableName');
  	$description = $this->_getParam('tableDefinition');
  	if(empty($name) || empty($description)) {
       $this->view->messageDisplay()->setMessage('Description is missing','error');
  		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','table','create',array('name' => $name, 'description' => $description));

    
    $creationOk = unserialize($data);
    
    if('KO' == $creationOk['status']) {
       $this->view->messageDisplay()->setMessage($creationOk['error']['message'],'error');
    } else {
       $this->view->messageDisplay()->setMessage('Table has succesfully been created','confirmation');	
	}
    $this->_forward('index');
  }

  function tableAction() {
  	$name = $this->_getParam('tableName');
  	if(empty($name)) {
  		$this->view->messageDisplay()->setMessage('mandatory data is missing','error');
		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','table','read',array('name' => $name));

    $tableData = unserialize($data);
    
    if('KO' == $tableData['status']) {
		$this->view->messageDisplay()->setMessage($tableData['error']['message'],'error'); 
		$this->_forward('index');
		return;	   	
    }
    
    $this->view->table = $tableData['table'];
	$this->view->tableData = $tableData;
	$this->view->tableName = $tableData['table']['name'];
		
	$this->saveBreadcrumbs($name,'','');
  } //end tableAction


  function createnewfoplineAction() {
  
  $name = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
  	$description = $this->_getParam('foplineDescription');

  	if(empty($name)  || empty($tableName)) {
	  	$this->view->messageDisplay()->setMessage('mandatory data is missing','error');
  		$this->_forward('index');
  		return;	
  	}
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','fopline','create',array('name' => $name,'tableName' => $tableName, 'description' => $description));

    $creationOK = unserialize($data);

	if('OK' == $creationOK['status']) $this->view->messageDisplay()->setMessage('Fopline has been successfully created','confirmation');
	else $this->view->messageDisplay()->setMessage($creationOK['error']['message'],'error');

    $this->_forward('table');
    
    	
  } //end createnewaction


  function foplineAction() {
  	$foplineName = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
	
  	if(empty($foplineName)  || empty($tableName)) {
  		$this->view->messageDisplay()->setMessage('missing mandatory data','error');
		$this->_forward('table');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','fopline','get',array('name' => $foplineName,'tableName' => $tableName));

    $foplineData = unserialize($data);

    if('KO' == $foplineData['status']) {
      $this->view->messageDisplay()->setMessage($foplineData['error']['message'],'error');
   	  $this->_forward('table');
  	  return;	   	
    }

Zend_Registry::get('app_log')->log($foplineData,7);

    $this->view->foplineName = $foplineData['fopline']['name'];
    $this->view->tableName = $foplineData['fopline']['tableName'];
    
    $this->view->description = $foplineData['fopline']['description'];

	$this->view->data = $foplineData;

    $this->getHelper('viewRenderer')->setNoRender(false);
    
    if('KO' == $foplineData['message']['status']) {
      $this->view->structure = array();
    } else {
      $this->view->structure = $foplineData['message']['message']['structure'];
    
    //Need to load some YUI javascript to perform the asynchronous call to test API
    $this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/pages/action.js', 'text/javascript');
    $this->view->YUILoader()->load(array('yahoo', 'dom', 'event','connection','json'));
    
    
    }//End message retrieval OK


    $this->saveBreadcrumbs($this->view->tableName,$this->view->foplineName,'');
        
  } //end actionAction
  
  
  
  
  
    function testapiAction() {
    
    $this->_helper->layout->disableLayout();
    $this->getHelper('viewRenderer')->setNoRender();
    
    $params = array();
	$params['tableName'] = $this->_getParam('tableName');
	$params['foplineName'] = $this->_getParam('foplineName');
  	$params['crypticEntry'] = $this->_getParam('crypticEntry'); 		
  

  	
  	$engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','fopline','validatecryptic',$params);


    $this->_helper->layout->disableLayout();
    $this->getHelper('viewRenderer')->setNoRender();
    
	$result = unserialize($data);
  	
  	if($data['status'] == 'KO') {
  		echo 'Issue occured: '.$data['error']['message'];
  		return;
  	}
  	
	if(!$result['validated']) {
		echo '<h3>Cryptic entry is not valid</h3>';
		return;	
	}
	
	echo '<h3>Cryptic entry is valid</h3>';

	if(!empty($result['matchingData'])) {
		foreach($result['matchingData'] as $name => $value) {
			echo '<b>'.$name.'</b>:'.$value.'<br />';	
		}	
	}

  	
  
  }
  
  
  
  
  function editfoplineAction() {

  	$foplineName = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
  	$id = $this->_getParam('id');
	
  	if(empty($foplineName) || empty($tableName)) {
      $this->view->messageDisplay()->setMessage('Missing mandatory pieces of data','error');
	  $this->_forward('table');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','message','get',array('foplineName' => $foplineName,'tableName' => $tableName,'id' => $id));

    $messageData = unserialize($data);

 	if('KO' == $messageData['status']) {
 	  $this->view->messageDisplay()->setMessage($messageData['error']['message'],'error');
 	  $this->_forward('fopline');
 	  return; 
 	}  

    $this->view->foplineName = $foplineName;
    $this->view->tableName = $tableName;

    //To be reviewed with javascript injector
$this->view->javascriptData('FoplineData',json_encode($messageData['message']));
 
 	if(!empty($id)) $this->view->id = $id;
    
    
    //We retrieve the definition of the different functions
    $functionsList = $engine->query('fop','function','list');    
    $functionsData = unserialize($functionsList);

 	if('KO' == $functionsData['status']) {
 	  $this->view->messageDisplay()->setMessage($functionsData['error']['message'],'error');
 	  $this->_forward('fopline');
 	  return; 
 	} 
 	//W inject the data in the HTML for use by Javascript
 	$this->view->javascriptData('functionsData',json_encode($functionsData['functions']));        
    
    
    

    $this->view->action = 'edit';
    
  	
    $this->getHelper('viewRenderer')->setNoRender(false);

	//Load javascript files to perform the magic	
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/tableConfig.js', 'text/javascript');    
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/tableTree.js', 'text/javascript');
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/tableTreeDD.js', 'text/javascript');
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/nodesList.js', 'text/javascript'); 
	$this->view->headScript()->appendFile($this->view->baseUrl().'/ressources/fop/javascript/pages/editAction.js', 'text/javascript');
  	$this->view->YUILoader()->load(array('yahoo', 'dom', 'event','treeview','connection','json','animation','dragdrop','container','datasource','autocomplete','datatable'));


    $this->saveBreadcrumbs( $this->view->tableName,$this->view->foplineName,'edit');

  }

  function savemessageAction() {
  	
  	$foplineName = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
    $messageDefinition = $this->_getParam('messageDefinition');
    
        
    if(empty($messageDefinition) || empty($foplineName) || empty($tableName)) {
      $this->view->messageDisplay()->setMessage('Mandatory information is missing','error');
 	  $this->_forward('fopline');
 	  return;	
    }
    $dataMessage = @json_decode($messageDefinition,true);
    
    if(empty($dataMessage)) {
      $this->view->messageDisplay()->setMessage('Message definition is badly formatted','error');
 	  $this->_forward('editfopline');
 	  return;	    	
    }
    $serializedMessage = serialize($dataMessage);
    

    
    
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','message','create',array(
      'foplineName' => $foplineName,
      'tableName' => $tableName,
      'message' => $serializedMessage,
      'messageFormat' => 'serializedPHP',
      'description' => $this->_getParam('messageChangeDescription')
      ));
    $updateOK = unserialize($data);
    
   	if('KO' == $updateOK['status']) $this->view->messageDisplay()->setMessage($updateOK['error']['message'],'error');
    else  $this->view->messageDisplay()->setMessage('message has been saved','confirmation');
 	$this->_forward('fopline');
    

  }
  

  
  
    function foplinehistoryAction() {
  	$foplineName = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
	
  	if(empty($foplineName) || empty($tableName)) {
		$this->view->messageDisplay()->setMessage('Mandatory information is missing','error');
  		$this->_forward('table');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','fopline','gethistory',array('name' => $foplineName,'tableName' => $tableName));
    $actionHistoryData = unserialize($data);

    if('KO' == $actionHistoryData['status']) {
      $this->view->messageDisplay()->setMessage($actionHistoryData['error']['message'],'error');
   	  $this->_forward('table');
  	  return;	   	
    }

Zend_Registry::get('app_log')->log($actionHistoryData,7);

    $this->view->foplineName = $actionHistoryData['fopline']['name'];
    $this->view->tableName = $actionHistoryData['fopline']['tableName'];
    
    $this->view->description = $actionHistoryData['fopline']['description'];

	$this->view->actionHistory = $actionHistoryData['history'];
	
	//used for the breadcrumb
	$this->view->action = 'history';
	
    $this->getHelper('viewRenderer')->setNoRender(false);
    
    $this->saveBreadcrumbs($this->view->tableName,$this->view->foplineName,'history');    
    
  } //end actionhistoryAction
  
  
  function makemessagecurrentAction() {
  	$foplineName = $this->_getParam('foplineName');
  	$tableName = $this->_getParam('tableName');
	$id =  $this->_getParam('id');
  	if(empty($foplineName) || empty($tableName) || empty($id)) {
  		$this->view->messageDisplay()->setMessage('Mandatory information is missing','error');
		$this->_forward('fopline');
  		return;	
  	}
  	
    $engine = Myzen_Oauth_Query_Singleton::getInstance();    
    $data = $engine->query('fop','message','makecurrent',array('foplineName' => $foplineName,'tableName' => $tableName,'id' => $id));
    $makeCurrentResultData = unserialize($data);  	
 
    if('KO' == $makeCurrentResultData['status']) {
      $this->view->messageDisplay()->setMessage($makeCurrentResultData['error']['message'],'error');
    } else {
      $this->view->messageDisplay()->setMessage('Message is now the current version','confirmation');    		
    }

   	$this->_forward('fopline');
  	return;
  	
  }
  
 
 
 
 	function saveBreadcrumbs($table,$fopline,$action) {
 		$crumbs = array();
 		if(empty($table)) $crumbs[0] = array('text' => 'FOP');
 		else  {
 			$crumbs[0] = array('text' => 'FOP', 'url'  => '/fop/');
	 		if(empty($fopline)) $crumbs[1] = array('text' => $table);
	 		else {
	 			$crumbs[1] = array('text' => $table, 'url'  => '/fop/'.$table.'/');
	 			if(empty($action)) $crumbs[2] = array('text' => $fopline);
	 			else  {
	 				$crumbs[2] = array('text' => $fopline, 'url'  => '/fop/'.$table.'/'.$fopline.'/');
	 				$crumbs[3] = array('text' => $action);
	 			}
	 		}
 		}		
	 
	 	$this->view->Breadcrumbs($crumbs);
 	} //end saveBreadcrumbs
 

} //End class

?>